﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CQIE.OVS.BLL;
using CQIE.OVS.Model;
using CQIE.OVS.Web.Models;

namespace CQIE.OVS.Web.Controllers
{
    public class UserController : Controller
    {
        // GET: User
        public int CreateManager(string Account, string passwrod)
        {

            UserBLL bll = new UserBLL();
            bool a = bll.GetByAccount(Account);
            int b;
            if (a == false)
            {
                b = 0;
            }
            else
            {
                b = 1;
            }
            if (b == 0)
            {
                User model = new User();
                model.Account = Account;
                model.Password = passwrod;
                model.root = 2;
                bll.Create(model);
            }
            return b;
        }

        

        /// <summary>
        /// 冻结
        /// </summary>
        /// <param name="ID"></param>
        public void IsActive(int ID)
        {
            UserBLL bll = new UserBLL();
            User model = bll.Get(ID);
            model.root = 0;
            bll.Update(model);
        }
        /// <summary>
        /// 解冻
        /// </summary>
        /// <param name="ID"></param>
        public void Active(int ID)
        {
            UserBLL bll = new UserBLL();
            User model = bll.Get(ID);
            model.root = 1;
            bll.Update(model);
        }
        /// <summary>
        /// 用户管理界面
        /// </summary>
        /// <returns></returns>
        public ActionResult UserManager()
        {
            return View();
        }
        /// <summary>
        /// 获取用户参与信息
        /// </summary>
        /// <param name="searchName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetUserInfoDetail(int page = 1, int limit = 10)
        {
            IList<QueryConditions> querys = new List<QueryConditions>();
            querys.Add(new QueryConditions("User", "=", Session["UserInfo"]));
            VoteBLL bll = new VoteBLL();
            int all = 0;
            IList<Vote> list = bll.GetPaged(querys, page, limit, out all);
            var data = list.Select(n => new VoteModel()
            {
                Title = n.Activity.Title,
                Name = n.Campaigner.Name,
                Time = n.VoteTime.ToString()
            });
            var dataOrder = new TableModel<VoteModel>()
            {
                data = data,
                message = "",
                status = 0,
                total = all,
            };
            return Json(dataOrder, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 用户参与前端视图
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult UserInfoDetail(int ID)
        {
            UserBLL infoBll = new UserBLL();
            User model = infoBll.Get(ID);
            Session["UserInfo"] = model;
            return View();
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="searchName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetList(string searchName = "", int page = 1, int limit = 10)
        {
            ViewBag.SearchName = searchName;
            IList<QueryConditions> querys = new List<QueryConditions>();
            querys.Add(new QueryConditions("Account", "like", string.Format("%{0}%", searchName)));
            querys.Add(new QueryConditions("root", ">=", "0"));

            UserBLL bll = new UserBLL();
            int all = 0;
            IList<User> list = bll.GetPaged(querys, page, limit, out all);
            var data = list.Select(n => new UserModel()
            {
                Account = n.Account,
                AcId = n.root > 1  ? "管理员" : "普通用户",
                IsActive = n.root > 0 ? "正常使用" : "冻结",
                ID = n.ID,
            });
            var dataOrder = new TableModel<UserModel>()
            {
                data = data,
                message = "",
                status = 0,
                total = all
            };
            return Json(dataOrder, JsonRequestBehavior.AllowGet);
        }
        
        
        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="passwrod"></param>
        public void UpdatePwd(string passwrod)
        {
            UserBLL bLL = new UserBLL();
            User model = Session["User"] as User;
            model.Password = passwrod;
            bLL.Update(model);
        }
       
    }

}